﻿using UnityEngine;
using System.Collections;

public class JohnnyUIButton : MonoBehaviour {

	public Sprite UpSprite;
	public Sprite DownSprite;
	public event System.EventHandler ClickUp;
	SpriteRenderer spriteRenderer;

	bool _enabled = true;
	public bool Enabled {
		get {
			return _enabled;
		}
		set {
			if (_enabled && !value) {
				this.Disable();
			}
			else if (!_enabled && value) {
				this.Enable();
			}
			_enabled = value;
		}
	}

	bool IsInSpriteBounds(Vector2 positionToCheck) {
		Vector3 minBounds = Camera.main.WorldToScreenPoint(this.spriteRenderer.bounds.min);
		Vector3 maxBounds = Camera.main.WorldToScreenPoint(this.spriteRenderer.bounds.max);
		if (positionToCheck.x >= minBounds.x && positionToCheck.x <= maxBounds.x && positionToCheck.y >= minBounds.y && positionToCheck.y <= maxBounds.y) {
			return true;
		}
		return false;
	}

	bool IsInSpriteBounds(Vector3 positionToCheck) {
		Vector3 minBounds = Camera.main.WorldToScreenPoint(this.spriteRenderer.bounds.min);
		Vector3 maxBounds = Camera.main.WorldToScreenPoint(this.spriteRenderer.bounds.max);
		if (positionToCheck.x >= minBounds.x && positionToCheck.x <= maxBounds.x && positionToCheck.y >= minBounds.y && positionToCheck.y <= maxBounds.y) {
			return true;
		}
		return false;
	}

	void Disable() {
		Color col = this.spriteRenderer.color;
		col.a = 0.5f;
		this.spriteRenderer.color = col;
	}

	void Enable() {
		Color col = this.spriteRenderer.color;
		col.a = 1f;
		this.spriteRenderer.color = col;
	}

	// Use this for initialization
	void Awake () {
		this.spriteRenderer = (SpriteRenderer)this.gameObject.GetComponent(typeof(SpriteRenderer));
	}
	
	// Update is called once per frame
	void Update () {
		if (!this.Enabled) {
			return;
		}
		if (JohnnyUIFunctions.IsInTouchState) {
			bool isInSprite = this.IsInSpriteBounds(JohnnyUIFunctions.FirstFingerCurrentPosition);
			bool isInSpriteInitially = this.IsInSpriteBounds(JohnnyUIFunctions.FirstFingerStartPosition);

			if (JohnnyUIFunctions.ClickDown) {
				if (isInSprite && isInSpriteInitially) {
					this.spriteRenderer.sprite = this.DownSprite;
				}
				else {
					this.spriteRenderer.sprite = this.UpSprite;
				}
			}
			else if (JohnnyUIFunctions.Moved) {
				if (isInSprite && isInSpriteInitially) {
					if (this.spriteRenderer.sprite == this.DownSprite && !isInSprite) {
						this.spriteRenderer.sprite = this.UpSprite;
					}
					else if (this.spriteRenderer.sprite == this.UpSprite && isInSprite) {
						this.spriteRenderer.sprite = this.DownSprite;
					}

				}
				else if (!isInSprite) {
					this.spriteRenderer.sprite = this.UpSprite;
				}
			}
			else if (JohnnyUIFunctions.ClickUp) {
				if (isInSprite && isInSpriteInitially) {
					this.spriteRenderer.sprite = this.UpSprite;
					if (this.ClickUp != null) {
						this.ClickUp(this, new System.EventArgs());
					}
				}
				else {
					this.spriteRenderer.sprite = this.UpSprite;
				}
			}
		}
	}
}
